﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Se.Extend
{
    public static class ExtendObject
    {
        public static Int16 ToInt16(this object obj)
        {
            if (true.Equals(obj))
                return 1;
            if (obj == null || string.IsNullOrEmpty(obj.ToString()) || false.Equals(obj))
                return 0;
            try
            {
                return Convert.ToInt16(obj);
            }
            catch
            {
                throw new ArgumentException(string.Format("\"{0}\" converted to type int16 fail.", obj));
            }
        }

        public static Int32 ToInt32(this object obj)
        {
            if (true.Equals(obj))
                return 1;
            if (obj == null || string.IsNullOrEmpty(obj.ToString()) || false.Equals(obj))
                return 0;
            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                throw new ArgumentException(string.Format("\"{0}\" converted to type int32 fail.", obj));
            }
        }

        public static Int64 ToInt64(this object obj)
        {
            if (true.Equals(obj))
                return 1;
            if (obj == null || string.IsNullOrEmpty(obj.ToString()) || false.Equals(obj))
                return 0;
            try
            {
                return Convert.ToInt64(obj);
            }
            catch
            {
                throw new ArgumentException(string.Format("\"{0}\" converted to type int64 fail.", obj));
            }
        }

        public static byte ToByte(this object obj)
        {
            if (true.Equals(obj))
                return 1;
            if (obj == null || string.IsNullOrEmpty(obj.ToString()) || false.Equals(obj))
                return 0;
            try
            {
                return Convert.ToByte(obj);
            }
            catch
            {
                throw new ArgumentException(string.Format("\"{0}\" converted to type byte fail.", obj));
            }
        }

        /// <summary>
        /// 将对象转换成双精度值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double ToDouble(object obj)
        {
            if (true.Equals(obj))
                return 1;
            if (obj == null || string.IsNullOrEmpty(obj.ToString()) || false.Equals(obj))
                return 0;
            try
            {
                return Convert.ToDouble(obj);
            }
            catch
            {
                throw new ArgumentException(string.Format("\"{0}\" converted to type double fail.", obj));
            }
        }
        /// <summary>
        /// 将对象转换成单精度值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object obj)
        {
            if (true.Equals(obj))
                return 1;
            if (obj == null || string.IsNullOrEmpty(obj.ToString()) || false.Equals(obj))
                return 0;
            try
            {
                return Convert.ToDecimal(obj);
            }
            catch
            {
                throw new ArgumentException(string.Format("\"{0}\" converted to type decimal fail.", obj));
            }
        }

        public static bool ToBool(this object obj)
        {
            if (obj == null || false.Equals(obj))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 返回不为NULL的字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToNotNullString(this object obj)
        {
            if (obj == null) return string.Empty;
            return obj.ToString();
        }

        public static DateTime ToDateTime(this object obj)
        {
            try
            {
                return Convert.ToDateTime(obj);
            }
            catch
            {
                throw new ArgumentException(string.Format("\"{0}\" converted to type DateTime fail.", obj));
            }
        }

        /// <summary>
        /// 转换为String(如果obj为null or empty,返回defaultString)
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultString"></param>
        /// <returns></returns>
        public static string ToString(this object obj, string defaultString)
        {
            if (obj == null) return defaultString;
            var str = obj.ToString();
            return string.IsNullOrEmpty(str) ? defaultString : str;
        }
    }
}
